package com.why;

import com.why.attributeinfo.BasicAttributeInfo;
import com.why.basictype.U2;
import com.why.basictype.U4;
import com.why.constantpool.ConstantPoolInfo;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author by xiaoTian
 * @date 2022/5/20.
 * 字节码文件的结构
 */
public class ClassFile {

    public U4 magic;
    public U2 minorVersion;
    public U2 majorVersion;
    public U2 constantPoolCount;
    public ConstantPoolInfo[] constantPoolInfos;
    public U2 accessFlags;
    public U2 thisClass;
    public U2 superClass;
    public U2 interfaceCount;
    public U2[] interfaces;
    public U2 fieldCount;
    public FieldInfos[] fieldInfos;
    public U2 methodsCount;
    public MethodInfo[] methodInfos;
    public U2 attributesCount;
    public BasicAttributeInfo[] attributeInfos;

    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("ClassFile{ \n")
                .append("magic: ")
                .append(magic.getHexValue() + "\n")
                .append("minorVersion: ")
                .append(minorVersion.getValue() + "\n")
                .append("majorVersion: ")
                .append(majorVersion.getValue()+ "\n")
                .append("constantPoolCount:")
                .append(constantPoolCount.getValue() + "\n")
                .append("constantInfo: \n");
        //常量池
        for (int i = 0; i < constantPoolInfos.length; i++) {
            stringBuffer.append("[" + (i + 1)+ "]:");
            if (constantPoolInfos[i] == null){
                stringBuffer.append("large numeric continued \n");
                continue;
            }
            stringBuffer.append(constantPoolInfos[i].toString() + "\n");
        }
        // 权限
        stringBuffer.append("accessFlags:")
            .append(accessFlags.getHexValue() + "\n")
            .append(thisClass.getHexValue() + "\n")
            .append(superClass.getHexValue() + "\n")
            .append(interfaceCount.getValue() + "\n");

        return stringBuffer.toString();
    }
}
